<?php
/* Abe Yang 10/5/2009
 *
 * Manual controls: p = main post id | n = number of recent side posts
 * Ex: http://kairosfellowship.org/newsletter.php?p=943&n=6
 *
 * Refer to: http://www.campaignmonitor.com/css/
 * Check this out: http://www.wilsonweb.com/wmt5/html-email-multi.htm
 *
 * This belongs in the theme directory, but in order to use it, there must
 * be a symbolic link from the wp/root directory. This can be done with 
 * this command (from wp directory):
 *
 * ln -s wp-content/themes/kairos/newsletter.php 
 * ************************************************************************ */

require('../../../wp-blog-header.php');

// variables
$mainpostcat = 3;									// cateogory id of mainposts
$mainpostid = ($_GET['p']) ? $_GET['p'] : 971;		// main post id
$recentsideposts = ($_GET['n']) ? $_GET['n'] : 2;	// number of most recent side posts

$quickhits = "
  	<strong>Combined Prayer Meeting</strong>
	<em>Tuesday 7:30pm @ First Pres</em>

   	<strong>Office hours with Sue</strong> 
	<em>Friday 12:30-3pm @ YWCA</em>
	<a href=\"mailto:suejyi@gmail.com\">Email Sue</a> for a half-hour spot!
   
   	<strong>Sunday Worship Service</strong>
	<em>Sunday 1pm @ Willard Middle School</em>
";

$misc = "
	<p>Remember to check out the <a href=\"http://kairosfellowship.org/\">Kairos website</a> throughout the week for updated news. Feel free to comment there as well!</p>
   	<p>Let me know if you're having issues viewing this newsletter. Oh, and if you'd like to unsubscribe to the Kairos mailing list, shoot me an email as well.</p>
";

$css = array(
	'body' => 'background-color: #1a0f04; margin: 0; padding: 0; font-family: Helvetica, Arial; color: #C1A045;',
	'table' => 'font-size: 12px; line-height: 14px;',
	'td' => 'text-align: justify; font-family: Helvetica, Arial; color: #C1A045;',
	'h2' => 'line-height: 18px;',
	'h3' => 'font-size: 14px; font-weight: normal; color: #AC4209; background-color: #332211; text-transform: uppercase;	margin: 0 0 8px 0; padding: 5px 8px;',
	'img' => 'border: 0; margin-bottom: 10px;',
	'strong' => 'color:#D4C484;',
	'a' => 'color: #44a0df; text-decoration: none;'
	);

/* DO NOT MODIFY BELOW */

// helper functions
function style($index) {
	global $css;
	return 'style="' . $css[$index] . '"';
}

function parsetag($index, $content) {
	global $css;
	$tag = '<' . $index;
	return str_replace($tag, $tag . ' ' . style($index), $content);
}

function parse($content) {
	$content = apply_filters('the_content', $content);
	$content = parsetag('h2', $content);
	$content = parsetag('a', $content);
	$content = parsetag('img', $content);
	$content = parsetag('strong', $content);
	return $content;
}

// render: main post
query_posts('p='.$mainpostid);
if (have_posts()) : while (have_posts()) : the_post(); 
	$title = get_the_title();
	$content = '<h2>' . linkto($title, get_permalink()) . '</h2>';
	$content .= get_the_content();
	
	// parsing time
	$content = parse($content);
endwhile; 
else : 
	$title = 'Kairos newsletter';
	$content = 'No post found';
endif; 

// render: side posts
$sidecontent = '';
query_posts('cat=-' . $mainpostcat . '&caller_get_posts=1&showposts=' . $recentsideposts);
if (have_posts()) : while (have_posts()) : the_post(); 
	// $sidecontent .= '<h2><a href="">' . get_the_title() . '</a></h2>';
	// assuming thumbnail exists... no checks here
	$thumbnail = get_post_custom_values('thumbnail');
	$sidecontent .= '<h2>' . linkto(get_the_title(), get_permalink()) . '</h2>';
	$sidecontent .= linktoimage($thumbnail[0], get_permalink());	
endwhile; 
endif;

if ($sidecontent) {
	$sidecontent = parse($sidecontent);
}
?>


<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title><?=$title?></title>

</head>
<body <?=style('body')?>>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
      <td align="center">
                  
         <table width="760" border="0" cellspacing="0" cellpadding="0">
            <tr>
               <td align="center">
                  
                  <table width="760" height="50" border="0" cellspacing="0" cellpadding="0">
                     <tr>
                        <td <?=style('td')?>>
                        	<h1>Kairos Newsletter</h1>
                        </td>
                     </tr>
                  </table>
                  
               </td>
            </tr>
         </table>
         
         <table width="760" border="0" cellspacing="0" cellpadding="0">
            <tr>
               <td width="500" align="left" valign="top">
                  <table width="100%" border="0" cellspacing="0" cellpadding="0" <?=style('table')?>>
                     <tr>
                        <td align="left" class="mainbar" <?=style('td')?>>
                           
                           <h3 <?=style('h3')?>>/// Feature</h3>
                           
                           <?=$content?>
                            
                        </td>
                     </tr>
                  </table>
                  
               </td>

			   <td width="20">&nbsp;</td>
               
               <td width="240" align="center" valign="top" class="sidebar">
                  <table width="100%" border="0" cellspacing="0" cellpadding="0" <?=style('table')?>>
                     <tr>
                        <td align="left" <?=style('td')?>>

							<h3 <?=style('h3')?>>/// Recent Side Posts</h3>
							
							<?=$sidecontent?>
						
                           	<h3 <?=style('h3')?>>/// Quick Hits</h3>
 							<?=parse($quickhits)?>
                           
                           	<h3 <?=style('h3')?>>/// Etcetera</h3>
							<?=parse($misc)?>
                           
                        </td>
                     </tr>
                  </table>
               </td>
            </tr>
         </table>
         
      </td>
   </tr>
</table>

</body>
</html>

